<?php

class db
{
	private $server, $username, $password = "", $db = "test", $connected = false, $result = null, $link;
	public $rows = 0, $cols = 0, $headers;
	
	public function CONN(/*string*/ $SERVER = "localhost", /*string*/ $USER = "root", /*string*/ $PASS = "usbw")
	{
		if ($SERVER != "localhost" || $this->server == null) { $this->server = $SERVER; }
		if ($USER != "root" || $this->username == null) { $this->username = $USER; }
		if ($PASS != "usbw" || $this->password == null) { $this->password = $PASS; }
		$this->connected = 
			($this->link = mysql_connect($this->server, $this->username, $this->password))
		? true : false;
	}
	
	public function SELECTDB(/*string*/ $NAME = null)
	{
		if ($NAME != null) { $this->db = $NAME; }
		if ($this->link == null) { $this->CONN(); }
		mysql_select_db($this->db, $this->link);
	}
	
	public function QUERY(/*string*/ $QUERY = null)
	{
		$this->result = mysql_query($QUERY);
		$this->rows = mysql_num_rows($this->result);
		$this->cols = count(mysql_fetch_array($this->result));
		return $this->result;
	}
	
	public function RESULT(/*string or array*/ $FIELDNAMES = "id", /*int*/ $INDEX = 0)
	{
		if ($this->result == null) { return null; }
		
		if (is_string($FIELDNAMES))
		{
			$result = mysql_result($this->result, $INDEX, $FIELDNAMES);
		}
		elseif (is_array($FIELDNAMES))
		{
			$maxi = count($FIELDNAMES);
			
			for ($i = 0; $i < $maxi; $i++)
			{
				$result[$FIELDNAMES[$i]] = $this->RESULT($FIELDNAMES[$i], $INDEX);
			}
		}
		else
		{
			$result = "error!";
		}
		
		return $result;
	}
	
	public function RESULTS(/*string or array*/ $FIELDNAMES = null)
	{
		if ($FIELDNAMES == null || $this->result == null) { return null; }
		
		if (is_string($FIELDNAMES))
		{
			for ($i = 0; $i < $this->rows; $i++)
			{
				$result[$i] = $this->RESULT($FIELDNAMES, $i);
			}
		}
		elseif (is_array($FIELDNAMES))
		{
			for ($i = 0; $i < $this->rows; $i++)
			{
				$result[$i] = $this->RESULT($FIELDNAMES, $i);
			}
		}
		else
		{
			$result = "error!";
		}
		
		return $result;
	}
	
	public function RESULTSTABLE(/*array*/ $FIELDNAMES = null, /*boolean*/ $SHOWHEADERS = true)
	{
		$data = $this->RESULTS($FIELDNAMES);
		
		$result = "<table>\n";
		
		if ($SHOWHEADERS)
		{
			$result .= "\t<tr>\n";
			
			for ($j = 0; $j < count($FIELDNAMES); $j++)
			{
				$result .= "\t\t<th>\n";
				
				$result .= "\t\t\t" . $FIELDNAMES[$j] . "\n";
				
				$result .= "\t\t</th>\n";
			}
			
			$result .= "\t</tr>\n";
		}
		
		$rows = mysql_num_rows($this->result);
		
		for ($i = 0; $i < $rows; $i++)
		{
			$result .= "\t<tr>\n";
			
			for ($j = 0; $j < count($FIELDNAMES); $j++)
			{
				$result .= "\t\t<td>\n";
				
				$result .= "\t\t\t" . $data[$j][$i] . "\n";
				
				$result .= "\t\t</td>\n";
			}
			
			$result .= "\t</tr>\n";
		}
		
		return $result .= "</table>\n";
	}
	
	public function __toString()
	{
		return ($this->connected ? "" : "no ") . "databaseconnection with '$this->db' under user '$this->username'" . mysql_error();
	}
	
	public function __construct(/*string*/ $SERVER = "localhost", /*string*/ $USER = "root", /*string*/ $PASS = "usbw")
	{
		$this->CONN($SERVER, $USER, $PASS);
	}
	
	public function __destruct()
	{
		//if ($this->connected) { mysql_close($this->link); }
	}
	
	public function __sleep()
	{
		return array('server', 'username', 'password', 'db');
	}
	
	public function __wakeup()
	{
		$this->CONN();
	}
}

?>